﻿using FastCloud.Base;
using FastCloud.Base.Config;
using FastCloud.Base.DB;
using FastCloud.Base.Desgin;
using FastCloud.Base.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.Base
{
    [Route("frame/base/group")]
    public class GroupController : BaseController
    {
        [AutoWired]
        private DB_Group DB_Group;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/Base/Group/Group_List.cshtml");
        }
        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/Base/Group/Group_Add.cshtml");
        }
        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/Base/Group/Group_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string GroupName = ValueConvert.ConvertToString(GetFormParam("GroupName"));
            string ParentGroupGuid = ValueConvert.ConvertToString(GetFormParam("ParentGroupGuid"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(GroupName))
            {
                sql += " and GroupName like @GroupName";
                ParamList.Add(new ParamEntity() { ParamName = "GroupName", ParamValue = "%" + GroupName + "%" });
            }
            if (!string.IsNullOrEmpty(ParentGroupGuid) && ParentGroupGuid != "Top")
            {
                sql += " and ParentGroupGuid = @ParentGroupGuid";
                ParamList.Add(new ParamEntity() { ParamName = "ParentGroupGuid", ParamValue = ParentGroupGuid });
            }

            return Result.OK(DB_Group.SelectPage("*", sql, "SortNum desc", ParamList));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Group Entity)
        {
            if (DB_Group.ExistGroup(Entity.GroupTag))
            {
                return Result.Error("已存在相同标识的分组！");
            }
            else
            {
                DB_Group.Insert(Entity);
                return Result.OK();
            }
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Group Entity)
        {
            Frame_Group DBEntity = DB_Group.SelectOne(Entity.GroupGuid);
            if (DBEntity.ParentGroupGuid != Entity.ParentGroupGuid)
            {
                List<Frame_Group> GroupList = DB_Group.SelectAllList();
                List<Frame_Group> ChildList = GroupList.FindAll((Frame_Group Group) => Group.ParentGroupGuid == Entity.GroupGuid);
                return Result.Error("存在子目录分组，无法修改父级组！");
            }
            if (DB_Group.ExistGroup(Entity.GroupGuid,Entity.GroupTag))
            {
                return Result.Error("已存在相同标识的分组！");
            }
            else
            {
                DB_Group.UpdateByID(Entity);
                return Result.OK();
            }
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string GroupGuid = ValueConvert.ConvertToString(GetFormParam("GroupGuid"));
            return Result.OK(DB_Group.SelectOne(GroupGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<Frame_Group> GroupList = DB_Group.SelectAllList();
            List<string> KeyList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            bool CanDelete = true;
            foreach (string Key in KeyList)
            {
                List<Frame_Group> ChildList = GroupList.FindAll((Frame_Group Group) => Group.ParentGroupGuid == Key);
                if (ChildList.Count > 0)
                {
                    return Result.Error("存在子目录分组，无法删除！");
                }
            }
            if (CanDelete)
            {
                DB_Group.DeleteBatch(KeyList);
            }
            return Result.OK();
        }


        [Route("tree"), HttpPost]
        public IActionResult Tree()
        {
            List<Frame_Group> GroupList = DB_Group.SelectAllList();
            GroupList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<TreeNode> NodeList = new List<TreeNode>();
            Frame_Group Root = new Frame_Group();
            Root.GroupGuid = "Top";
            Root.GroupName = "全部分类";
            Root.ParentGroupGuid = "";
            GroupList.Add(Root);
            foreach (Frame_Group item in GroupList)
            {
                TreeNode node = new TreeNode();
                node.ID = item.GroupGuid;
                node.Name = item.GroupName;
                node.PID = item.ParentGroupGuid;
                node.AddExtra("Tag", item.GroupTag);
                if (item.GroupGuid == Root.GroupGuid)
                {
                    node.IsTop = true;
                    node.Open = true;
                }
                NodeList.Add(node);
            }
            return Result.OK(CloudUtil.BuildTree(NodeList));
        }

        [Route("grouptree"), HttpPost]
        public IActionResult GroupTree()
        {
            string GroupTag = ValueConvert.ConvertToString(GetFormParam("GroupTag"));
            List<Frame_Group> GroupList = DB_Group.SelectAllList();
            GroupList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<TreeNode> NodeList = new List<TreeNode>();
            foreach (Frame_Group item in GroupList)
            {
                TreeNode node = new TreeNode();
                node.ID = item.GroupGuid;
                node.Name = item.GroupName;
                node.PID = item.ParentGroupGuid;
                node.AddExtra("Tag", item.GroupTag);
                if (item.GroupTag == GroupTag)
                {
                    node.IsTop = true;
                    node.Open = true;
                }
                NodeList.Add(node);
            }
            return Result.OK(CloudUtil.BuildGroupTree(NodeList));
        }
    }
}
